package com.p595263720.o_amap.nav;

import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;


import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviIndependentRouteListener;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.AmapPageType;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviPath;
import com.amap.api.navi.model.AMapNaviPathGroup;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.model.NaviPoi;
import com.google.gson.Gson;
import com.p595263720.o_amap.OAmapPlugin;
import com.p595263720.o_amap.nav.model.CalculateResult;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class NavHandlers implements MethodChannel.MethodCallHandler, AMapNaviListener {

    // 获取导航Manager
    private AMapNavi mAMapNavi;

    private MethodChannel.Result result;
    private Context mContext;

    public NavHandlers(Context context) {
        mContext = context;
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        this.result = result;
        switch (call.method) {
            case "calculateDriveRoute":
                calculateDriveRoute(call);
                break;
            case "startNav":
                startNav(call);
                break;
        }
    }


    public void startNav(MethodCall call) {
        try {
            double lat = call.argument("lat");
            double lon = call.argument("lon");
            String name = call.argument("name");
            if (lat > 0 && lon > 0) {
                Poi poi = new Poi(name, new LatLng(lat, lon), "");
                AmapNaviPage.getInstance().showRouteActivity(mContext, new AmapNaviParams(
                        null, null, poi, AmapNaviType.DRIVER, AmapPageType.ROUTE
                ), null);
                this.result.success(null);
            } else {
                Toast.makeText(mContext, "经纬度不能为空", Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {

        }

    }

    //周边搜索
    public void calculateDriveRoute(MethodCall call) {
        mAMapNavi = AMapNavi.getInstance(mContext);
        double startLat = call.argument("startLat");
        double startLon = call.argument("startLon");
        double endLat = call.argument("endLat");
        double endLon = call.argument("endLon");
        int naviStrategy = call.argument("naviStrategy");
        // 起点信息
        List<NaviLatLng> startList = new ArrayList<NaviLatLng>();
        startList.add(new NaviLatLng(startLat, startLon));
        // 终点信息
        List<NaviLatLng> endList = new ArrayList<NaviLatLng>();
        endList.add(new NaviLatLng(endLat, endLon));
        // 经纬度算路
        mAMapNavi.calculateDriveRoute(startList, endList, null, naviStrategy);
        mAMapNavi.addAMapNaviListener(this);
    }


    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onInitNaviSuccess() {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onGetNavigationText(String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

    @Override
    public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {

    }

    @Override
    public void hideModeCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {

    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }

    @Override
    public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {
        try {
            HashMap<Integer, AMapNaviPath> naviPaths = mAMapNavi.getNaviPaths();
            List<CalculateResult> list = new ArrayList<CalculateResult>();
            for (AMapNaviPath aMapNaviPath : naviPaths.values()) {
                list.add(new CalculateResult(aMapNaviPath));
            }
            HashMap<String, List<CalculateResult>> result = new HashMap<String, List<CalculateResult>>();
            result.put("result", list);
            this.result.success(JSON.toJSONString(result));
        } catch (Exception e) {
        }
    }

    @Override
    public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {
        Toast.makeText(mContext, "路线规划失败", Toast.LENGTH_SHORT);
    }

    @Override
    public void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {

    }

    @Override
    public void onGpsSignalWeak(boolean b) {

    }
}
